package com.bysj.lms.service.impl;

import com.bysj.lms.pojo.dto.LmsEmDto;
import com.bysj.lms.pojo.entity.LmsEm;
import com.bysj.lms.mapper.LmsEmMapper;
import com.bysj.lms.pojo.entity.LmsFa;
import com.bysj.lms.service.ILmsEmService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bysj.lms.service.ILmsFaService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiaojiakai
 * @since 2022-02-04
 */
@Service
public class LmsEmServiceImpl extends ServiceImpl<LmsEmMapper, LmsEm> implements ILmsEmService {
    
    @Autowired
    LmsEmMapper lmsEmMapper;
    @Autowired
    ILmsFaService lmsFaService;
    @Override
    public PageInfo getList(Integer page, Integer count,String status) {
        PageHelper.startPage(page,count);
        List<LmsEmDto> lmsEmDtos = lmsEmMapper.getList(status);
        return new PageInfo(lmsEmDtos);
    }

    @Override
    public boolean apply(LmsEm lmsEm) {
        lmsEm.setDiscoverTime(LocalDateTime.now());
        lmsEm.setStatus("0");
        boolean save = save(lmsEm);
        if(lmsEm.getType().equals("0")){
            lmsFaService.lambdaUpdate().eq(LmsFa::getInstruNumber,lmsEm.getInstrumentId())
                    .set(LmsFa::getPreSituation,"异常").update();
        }
        return save;
    }

    @Override
    public boolean handle(LmsEm lmsEm) {
        lmsEm.setStatus("1");
        lmsEm.setHandleStartTime(LocalDateTime.now());
        boolean update = updateById(lmsEm);
        if(lmsEm.getType().equals("0")){
            lmsFaService.lambdaUpdate().eq(LmsFa::getInstruNumber,lmsEm.getInstrumentId())
                    .set(LmsFa::getPreSituation,"维修中").update();
        }
        return update;
    }

    @Override
    public boolean finish(LmsEm lmsEm) {
        boolean update = lambdaUpdate().eq(LmsEm::getId, lmsEm.getId())
                .set(LmsEm::getHandleProcess, lmsEm.getHandleProcess()).set(LmsEm::getHandleResult, lmsEm.getHandleResult())
                .set(LmsEm::getStatus, "2")
                .update();
        if(lmsEm.getType().equals("0")){
            lmsFaService.lambdaUpdate().eq(LmsFa::getInstruNumber,lmsEm.getInstrumentId())
                    .set(LmsFa::getPreSituation,"在用").update();
        }
        return update;
    }
}
